import {
	DEViewActionType,
	IContext,
	IDECtrlAbility,
	IDEViewAbility,
	IDEViewController,
	IDEViewControllerParams,
	IDEViewStore,
	IParam,
	IViewActionResult,
	IViewDetail,
	IViewModel,
} from '@/core/interface';
import { AuthUtil, computedNavData, deepCopy } from '@/core/utils';
import { ViewController } from './view-controller';

/**
 * 实体视图控制器
 *
 * @export
 * @class DEViewController
 * @extends {ViewController<T, S, A>}
 * @template T 视图行为接口
 * @template S 视图仓库数据接口
 * @template A 视图能力接口
 */
export class DEViewController<
		T,
		S extends IDEViewStore,
		A extends IDEViewAbility
	>
	extends ViewController<T | DEViewActionType, S, A>
	implements IDEViewController<S, A>
{
	/**
	 * 处理视图初始化
	 *
	 * @protected
	 * @param {IDEViewControllerParams<T, A>} params
	 * @memberof DEViewController
	 */
	protected processViewInit(params: IDEViewControllerParams<T, A>) {
		super.processViewInit(params);
		Object.assign(this.store, {
			openView: this.openData.bind(this),
			newView: this.newData.bind(this),
			toolbarItems: this.model.toolbarItems || [],
		});
	}

	/**
	 * 打开数据
	 *
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @return {*}
	 * @memberof DEViewController
	 */
	public openData(context: IContext, viewParams: IParam, data: IParam[]) {
		const _data = data.length > 0 ? data[data.length - 1] : {};
		const openDataModel = this.getModel<IViewModel>().openData;
		if (!openDataModel) {
			App.getNotificationHelper().warning(App.ts('app.notificationtitle.warn','警告'), App.ts('app.common.noconfigmainview','未配置主数据视图'));
			return;
		}
		const {
			redirectView,
			openDataViewName,
			navContext,
			navViewParam,
			deCodeName,
		} = openDataModel;
		if (deCodeName && _data[deCodeName]) {
			Object.assign(context, { [deCodeName]: _data[deCodeName] });
		}
		const _context = deepCopy(context);
		const _viewParams = deepCopy(viewParams);
		if (navContext) {
			const navData = computedNavData(_data, context, viewParams, navContext);
			navData && Object.assign(_context, navData);
		}
		if (navViewParam) {
			const navData = computedNavData(_data, context, viewParams, navViewParam);
			navData && Object.assign(_viewParams, navData);
		}
		//  todo 工作流已读标记
		if (redirectView) {
			// 处理重定向视图跳转
		}
		if (openDataViewName) {
			const view: IViewDetail = App.getAppViewConfig()?.[openDataViewName];
			if (!view) {
				console.warn(App.ts("app.common.noeditview","编辑视图不存在"));
				return;
			}
			App.getOpenViewHelper().openPage(view, _context, _viewParams);
		}
	}

	/**
	 * 新建数据
	 *
	 * @param {IContext} context
	 * @param {IParam} viewParams
	 * @param {IParam[]} data
	 * @return {*}
	 * @memberof DEViewController
	 */
	public newData(context: IContext, viewParams: IParam, data: IParam[]) {
		const _data = data.length > 0 ? data[data.length - 1] : {};
		const newDataModel = this.getModel<IViewModel>().newData;
		if (!newDataModel) {
			App.getNotificationHelper().warning(App.ts("app.notificationtitle.warn","警告"), App.ts("app.common.noconfignewview","未配置新建数据视图"));
			return;
		}
		const {
			enableWizardAdd,
			enableBatchAdd,
			batchAddOnly,
			newDataViewName,
			entityName,
			navContext,
			navViewParam,
		} = newDataModel;
		if (enableWizardAdd) {
			// todo
		} else if (enableBatchAdd) {
			// todo
		} else if (batchAddOnly) {
			// todo
		} else if (newDataViewName) {
			const _context = deepCopy(context);
			const _viewParams = deepCopy(viewParams);
			// 新建视图且不是拷贝模式时 删除上下文主键信息
			if (
				entityName &&
				_context[entityName.toLowerCase()] &&
				!viewParams.copymode
			) {
				delete _context[entityName.toLowerCase()];
			}
			if (navContext) {
				const navData = computedNavData(data, context, viewParams, navContext);
				navData && Object.assign(_context, navData);
			}
			if (navViewParam) {
				const navData = computedNavData(
					_data,
					context,
					viewParams,
					navViewParam
				);
				navData && Object.assign(_viewParams, navData);
			}
			const view: IViewDetail = App.getAppViewConfig()?.[newDataViewName];
			if (!view) {
				console.warn(App.ts("app.common.nonewview","新建视图不存在"));
				return;
			}
			App.getOpenViewHelper().openPage(view, _context, _viewParams);
		}
	}
	/**
	 * 计算工具栏权限状态
	 *
	 * @protected
	 * @param {IParam} data
	 * @memberof MDViewController
	 */
	protected async calcToolbarItemAuthState(data: IParam) {
		if (this.model.entityCodeName) {
			const UIService = await App.getUIService(
				this.model.entityCodeName,
				this.store.context
			);
			AuthUtil.calcToolbarItemAuthState(
				data,
				this.store.toolbarItems,
				UIService
			);
		}
	}

	/**
	 * 加载视图
	 *
	 * @memberof DEViewController
	 */
	public load(args?: IParam): Promise<IViewActionResult> {
		if (this.model.xDataCtrlName) {
			const ability = this.getSubAbility<IDECtrlAbility>(
				this.model.xDataCtrlName
			);
			if (ability) {
				return ability.load(args);
			}
		}
		return Promise.resolve({ ok: false, rowData: {}, data: [] });
	}
}
